package com.yumao.animatordemo;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.ImageView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class Main4Activity extends AppCompatActivity implements View.OnClickListener {
    int[] imageId = {R.id.one, R.id.two, R.id.three, R.id.four, R.id.five, R.id.six, R.id.seven};
    private List<ImageView> list;
    private boolean isShow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main4);
        list = new ArrayList<>();
        for (int i = 0; i < imageId.length; i++) {
            ImageView im = (ImageView) findViewById(imageId[i]);
            im.setOnClickListener(this);
            list.add(im);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.one:
                if (isShow) {
                    closeAnimator();
                } else {
                    startAnimator();
                }
                isShow = !isShow;
                break;
            default:
                Toast.makeText(this, "" + view.getId(), Toast.LENGTH_SHORT).show();
                break;
        }
    }

    private void startAnimator() {
        float angle = 90 / (imageId.length - 2);
        float radius = 800;
        for (int i = 1; i < imageId.length; i++) {
            float ang = angle * (i - 1);
            double d = Math.PI * ang / 180;
            float translationX = (float) (radius * Math.cos(d));//cos 和 sin 方法中的角为弧度制
            float translationY = (float) (radius * Math.sin(d));
            ObjectAnimator animatorX = ObjectAnimator.ofFloat(list.get(i), "translationX", 0f, translationX);
            ObjectAnimator animatorY = ObjectAnimator.ofFloat(list.get(i), "translationY", 0f, translationY);
            AnimatorSet set = new AnimatorSet();
            set.setDuration(500);
            set.setInterpolator(new AccelerateDecelerateInterpolator());
            set.playTogether(animatorX, animatorY);
            if (i == 1) {
                set.addListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        super.onAnimationEnd(animation);
                        list.get(0).setClickable(true);
                    }

                    @Override
                    public void onAnimationStart(Animator animation) {
                        super.onAnimationStart(animation);
                        list.get(0).setClickable(false);
                    }
                });
            }
            set.start();
        }
    }

    private void closeAnimator() {
        float angle = 90 / (imageId.length - 2);
        float radius = 800;
        for (int i = 1; i < imageId.length; i++) {
            float ang = angle * (i - 1);
            double d = Math.PI * ang / 180;
            float translationX = (float) (radius * Math.cos(d));//cos 和 sin 方法中的角为弧度制
            float translationY = (float) (radius * Math.sin(d));
            ObjectAnimator animatorX = ObjectAnimator.ofFloat(list.get(i), "translationX", translationX, 0f);
            ObjectAnimator animatorY = ObjectAnimator.ofFloat(list.get(i), "translationY", translationY, 0f);
            AnimatorSet set = new AnimatorSet();
            set.setDuration(500);
            set.setInterpolator(new AccelerateDecelerateInterpolator());
            set.playTogether(animatorX, animatorY);
            if (i == 1) {
                set.addListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        super.onAnimationEnd(animation);
                        list.get(0).setClickable(true);
                    }

                    @Override
                    public void onAnimationStart(Animator animation) {
                        super.onAnimationStart(animation);
                        list.get(0).setClickable(false);
                    }
                });
            }
            set.start();
        }
    }
}
